//----
// PART VII - Withholding


	/* This file uses SOI Form 8805 data along with 
	administrative data from Forms 1042S and 8288A
	to describe amounts and trends in withholding on 
	partnership payments to haven jurisdictions.  It also 
	produces Figures 15. */

// ----------------
// US Taxes Withheld on Flows

	//-----
	// Prelim: Build Dataset of Form 8805 Taxes from public SOI data
	
	
	/* Import SOI publicly available data */
	
	clear
	
	import delimited ${oth_dir}/soi_form_8805_table.csv, clear
	
	foreach v of varlist number total_taxable_inc income loss ///
		us_tax_withheld {
			destring `v', replace force
		}
	
	// Categorize by destination
	
		g haven = 0
		foreach c in "CJ" /// Cayman
			"AV" "BF" "BA" "BD" "VI" ///
			"GK" "IM" "JE" "MV" "PC" "AE" /// Zero Rate
			"AN" "AC" "AA" "BB" "BH" "CW" "CS" ///
			"UC" "CY" "DJ" "DO" "GI" "GJ" "HK" ///
			"JO" "LE" "LI" "LS" "LU" "MC" "MT" ///
			"RM" "MP" "FM" "MN" "MH" "NR" "NE" ///
			"PM" "SM" "SE" "NN" "SC" "ST" "VC" /// 
			"TW" "TN" "TK" "NH" "WS" /// Other
			"EI" "NL" "SN" "SZ" /// Conduits
			{
				replace haven = 1 if country_code=="`c'"
			}
		
		// Keep havens
		
		keep if haven==1
		
		// Collapse
		
		gcollapse (sum) us_tax_withheld, by(year)
		
		// Clean and deflate
		
		rename us_tax_withheld f8805_withholding
		
		merge 1:1 year using ${temp_dir}/gdp_deflator.dta, ///
			nogen keep(1 3)
			
		replace f8805_withholding = f8805_withholding*gdp_deflator
		
		drop gdp_deflator
		
	save ${temp_dir}/soi_form_8805_withholding.dta, replace

	
	
	// Load and clean Form 1042-S data
	
	clear
	
	use ${data_dir}/form_1042s_withholding_by_destination.dta, clear
		
		// Narrow scope to havens
		
		keep if inlist(destination,"Cay","Zer","Hav","Con")
		
		// Collapse
		
		gcollapse (sum) tax_withheld, by(year)
		
		// Clean and deflate
		
		rename tax_withheld f1042s_withholding		
		
		merge 1:1 year using ${temp_dir}/gdp_deflator.dta, ///
			nogen keep(1 3)
			
		replace f1042s_withholding = f1042s_withholding*gdp_deflator
		
		drop gdp_deflator
		
		save ${temp_dir}/form_1042s_for_merging.dta, replace


	// Load and clean Form 8288 data
	
	clear
	
	use ${data_dir}/form_8288a_withholding_by_destination.dta, clear
		
		// Narrow scope
		
		keep if inlist(destination,"Cay","Zer","Hav","Con")
		
		// Collapse
		
		gcollapse (sum) tax_withheld, by(year)
		
		// Clean and deflate
		
		rename tax_withheld f8288a_withholding
		
		merge 1:1 year using ${temp_dir}/gdp_deflator.dta, ///
			nogen keep(1 3)
			
		replace f8288a_withholding = f8288a_withholding*gdp_deflator
		
		drop gdp_deflator
		
		save ${temp_dir}/form_8288a_for_merging.dta, replace

	
	// Merge the data
	
	use ${temp_dir}/form_1042s_for_merging.dta, clear
	
		merge 1:1 year using ${temp_dir}/soi_form_8805_withholding.dta, ///
			nogen keep(1 3)
	
		merge 1:1 year using ${temp_dir}/form_8288a_for_merging.dta, ///
			nogen keep(1 3)
			
	// Figure 15

	// clean
		
		foreach v of varlist f* {
			replace `v' = `v'/1e9
			format `v' %4.2f
		}
	
	// grayscale
	
	graph bar f1042s f8805 f8288, ///
		over(y, gap(10) label(labsize(small))) ///
		stack ///
		bar(1, color(gray*.75)) ///
		bar(2, color(gray*1.5)) ///
		bar(3, color(gray*4)) ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Billions of 2020 Dollars", size(msmall) height(5)) ///
		ylabel(0(1)5.25, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "US source (1042-S)" 2 "ECI (8805)" 3 "FIRPTA (8288-A)") size(msmall) region(lcolor(white)))		
		graph export ${fig_dir}/fig_withh_collected.png, replace width(1000)
	
	// color
	
	graph bar f1042s f8805 f8288, ///
		over(y, gap(10) label(labsize(small))) ///
		stack ///
		bar(1, color("133 41 40")) ///
		bar(2, color("237 144 90")) ///
		bar(3, color(dkgreen)) ///
		plotregion(lcolor(black)) ///
		graphregion(fcolor(white) lcolor(white)) ///
		bgcolor(white) ///
		ytitle("Billions of 2020 Dollars", size(msmall) height(5)) ///
		ylabel(0(1)5.25, labsize(msmall)) ///
		leg(pos(6) cols(3) order (1 "US source (1042-S)" 2 "ECI (8805)" 3 "FIRPTA (8288-A)") size(msmall) region(lcolor(white)))		
		graph export ${fig_dir}/fig_withh_collected_color.png, replace width(1000)

	// Export
			
		foreach v of varlist f* {
			replace `v'= round(`v',.01)
			}
	
		export delimited using ${out_dir}/fig15_tab.csv, replace
